package com.anke.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.anke.constant.MessageConstant;
import com.anke.entity.Reservation;
import com.anke.entity.Result;
import com.anke.entity.Staff;
import com.anke.entity.User;
import com.anke.service.UserService;
import com.github.pagehelper.PageHelper;
import org.junit.Test;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;

@RestController// 和controller注解的不同在于其中的方法返回的都是json
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Reference
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        System.out.println(user);
        System.out.println("请求注册");
        try {
            userService.register(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.REGISTER_FAIL);
        }
        return new Result(true, MessageConstant.REGISTER_SUCCESS);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        try {
            if (userService.login(user)) {
                return new Result(true, MessageConstant.LOGIN_SUCCESS);
            }
            return new Result(false,MessageConstant.LOGIN_FAIL);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }

    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody User user){
        try {
            userService.changePassword(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CHANGE_PWORD_FAIL);
        }
        return new Result(true , MessageConstant.CHANGE_PWORD_SUCCESS);
    }

    @RequestMapping("/findInformation")
    public Result findInformation(@RequestBody User user){
        try {
            List<User> u = userService.findInformation(user);
            if (u.isEmpty()) {
                return new Result(false, MessageConstant.FIND_INFORMATION_FAIL);
            }
            return new Result(true, MessageConstant.FIND_INFORMATION_SUCCESS, u);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }

    @RequestMapping("/updateInformation")
    public Result updateInformation(@RequestBody User user){
        try {
            userService.updateInformation(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CHANGE_INFORMATION_FAIL);
        }
        return new Result(true , MessageConstant.CHANGE_INFORMATION_SUCCESS);
    }

    @RequestMapping("/findAllStaff")
    public Result findAllStaff(){
        try {
            PageHelper.startPage(1,2);
            List<Staff> s = userService.findAllStaff();
            if (s.isEmpty()) {
                return new Result(false,MessageConstant.FIND_ALL_STAFF_FAIL);
            }
            return new Result(true, MessageConstant.FIND_ALL_STAFF_SUCCESS, s);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }

    @RequestMapping("/findBySkill")
    public Result findBySkill(@RequestBody String skill){
        try {
            List<Staff> s = userService.findBySkill(skill);
            if(s.isEmpty()){
                return new Result(false, MessageConstant.FIND_STAFF_FAIL);
            }
            return new Result(true,MessageConstant.FIND_STAFF_SUCCESS,s);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SYSTEM_EXCEPTION);
        }
    }

    @RequestMapping("/reservation")
    public Result reservation(@RequestBody Reservation reservation){
        try {
            userService.reservation(reservation);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.RESERVATION_SUCCESS);
        }
        return new Result(true, MessageConstant.RESERVATION_FAIL);
    }
}
